# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from fastapi import APIRouter, Request, Depends
from hypertext import response_json
from common.enums.apply import DrawsEnum
from common.utils.config import ConfigUtil
from apps.api.schemas.apply.draws import drawing_schema as schema
from apps.api.service.apply.draws.drawing_service import DrawingService
from apps.api.service.apply.draws.drawing_service import DrawsNotifyService
from common.service.subscribe_logic import SubscribeLogic

router = APIRouter(prefix="/drawing", tags=["绘画生成接口"])


@router.get("/config", summary="绘画配置")
@response_json
async def config(request: Request, params: schema.DrawConfigIn = Depends()):
    user_id: int = request.state.user_id

    data = {
        "options": [],
        "configs": {
            "status": False,
            "points": 0,
            "surplus_num": 0,
            "free_status": False
        }
    }

    # 订阅特权
    member = await SubscribeLogic.get_surplus_free(user_id=user_id, scene="draws", mark=params.engine)
    if member:
        data["configs"]["surplus_num"] = member.get("surplus_num")
        data["configs"]["free_status"] = member.get("free_status")

    # MJ配置
    mj = await ConfigUtil.get("draws", "mj") or {}
    if mj.get("status"):
        name = mj.get("name") or "MJ绘画"
        data["options"].append({"type": "mj", "name": name})

    # 豆包配置
    doubao = await ConfigUtil.get("draws", "doubao") or {}
    if doubao.get("status"):
        name = doubao.get("name") or "豆包绘画"
        data["options"].append({"type": "doubao", "name": name})

    # Dalle配置
    dalle = await ConfigUtil.get("draws", "dalle") or {}
    if dalle.get("status"):
        name = dalle.get("name") or "DALLE绘画"
        data["options"].append({"type": "dalle", "name": name})

    # SD配置
    sd = await ConfigUtil.get("draws", "sd") or {}
    if sd.get("status"):
        name = sd.get("name") or "SD绘画"
        data["options"].append({"type": "sd", "name": name})

    # 场景处理
    if params.engine == DrawsEnum.ENGINE_MJ:
        mj_mode = []
        mode = mj.get("mode") or {}
        conf = DrawsEnum.mj_config(code=True)
        for k, v in conf["mj_mode"].items():
            if mode.get(k, {}).get("status"):
                points = int(mode.get(k, {}).get("points", 0))
                mj_mode.append({"type": k, "name": v, "points": points})
        conf["mj_mode"] = mj_mode
        data["configs"]["status"] = True
        data["configs"]["points"] = 0
        data["configs"].update(conf)
    elif params.engine == DrawsEnum.ENGINE_SD:
        data["configs"]["status"] = True
        data["configs"]["points"] = int(dalle.get("points", 0))
    elif params.engine == DrawsEnum.ENGINE_DALLE:
        data["configs"]["status"] = True
        data["configs"]["points"] = int(dalle.get("points", 0))
    elif params.engine == DrawsEnum.ENGINE_DOUBAO:
        data["configs"]["status"] = True
        data["configs"]["points"] = int(doubao.get("points", 0))
        data["configs"].update(DrawsEnum.doubao_config(code=True))

    return data


@router.post("/generate", summary="图片生成")
@response_json
async def generate(request: Request, params: schema.DrawGenerateIn):
    user_id: int = int(request.state.user_id)
    await DrawingService(request, user_id, params).generate()


@router.post("/notify_mj", summary="Mj回调")
@response_json
async def notify_mj(request: Request):
    params = await request.json()
    await DrawsNotifyService.notify_mj(params)
